package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.Sms_Coupon_Spu_Category_Relation_Mapper;
import com.shop.service.Sms_Coupon_Spu_Category_Relation_Service;
import com.shop.pojo.sms.Sms_Coupon_Spu_Category_Relation;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Sms_Coupon_Spu_Category_Relation_ServiceImpl implements Sms_Coupon_Spu_Category_Relation_Service {

    @Autowired
    private Sms_Coupon_Spu_Category_Relation_Mapper sms_coupon_spu_category_relation_mapper;

    /**
     * 后台优惠卷分类关系维护显示
     *
     * @param page
     * @param spc
     * @return
     */
    @Override
    public LayuiUtils<Sms_Coupon_Spu_Category_Relation> find_admin_sms_coupon_category(Page<Sms_Coupon_Spu_Category_Relation> page, Sms_Coupon_Spu_Category_Relation spc) {
        LayuiUtils<Sms_Coupon_Spu_Category_Relation> layuiUtils = new LayuiUtils<>();

        QueryWrapper<Sms_Coupon_Spu_Category_Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id", spc.getCoupon_id());
        if (spc.getCategory_name() != null) {
            queryWrapper.like("category_name", spc.getCategory_name());
        }

        Page<Sms_Coupon_Spu_Category_Relation> spcs = sms_coupon_spu_category_relation_mapper.selectPage(page, queryWrapper);


        layuiUtils.setData(spcs.getRecords());
        layuiUtils.setCount(spcs.getTotal());

        return layuiUtils;
    }


    /**
     * 后台优惠卷分类的关联关系添加
     *
     * @param spc
     * @return
     */
    @Override
    public R insert_admin_sms_coupon_category(Sms_Coupon_Spu_Category_Relation spc) {

        QueryWrapper<Sms_Coupon_Spu_Category_Relation> query = new QueryWrapper<>();
        if (spc.getCategory_id() != null) {
            query.eq("category_id", spc.getCategory_id());
        }
        //判断添加的分类名称是否重复
        List<Sms_Coupon_Spu_Category_Relation> sms_coupon_spu_category_relation = sms_coupon_spu_category_relation_mapper.selectList(query);
        System.out.println(sms_coupon_spu_category_relation);
        if (sms_coupon_spu_category_relation.size() == 0) {
            int insert = sms_coupon_spu_category_relation_mapper.insert(spc);

            if (insert > 0) {
                return R.success();
            }
            return R.error(500, "添加失败");

        }

        return R.error(500, "该分类已存在");

    }

    /**
     * 后台删除优惠卷分类的关联关系
     *
     * @param id
     * @return
     */
    @Override
    public R del_admin_sms_coupon_category(List<Integer> id) {

        int i = sms_coupon_spu_category_relation_mapper.deleteBatchIds(id);

        if (i > 0) {
            return R.success();
        }
        return R.error(500, "删除失败");
    }

    /**
     * 后台优惠卷分类修改维护关系
     *
     * @param spc
     * @return
     */
    @Override
    public R up_admin_sms_coupon_category(Sms_Coupon_Spu_Category_Relation spc) {
        int i = sms_coupon_spu_category_relation_mapper.updateById(spc);
        if (i > 0) {
            return R.success();
        }
        return R.error(500, "修改失败");
    }

}
